Flash Article

Data for Designers: Connecting to Data in Flash Using Data Wizards


Table of Contents

  1. Introduction
  2. Getting the Required Software
  3. Understanding the Data Connection Wizard
  4. Syndicating XML with the Data Connection Wizard
  5. Using the DataGrid Column Editor
  6. Consuming Web Services Using Wizards

Consuming Web Services Using Wizards

In addition to parsing and binding XML packets to a DataGrid, the Data Connection wizard binds results from a web service. The wizard doesn't work equally well with all web services however; some require more massaging than other data formats.

When I tested web services, I had about 13 web services defined in the Web Services panel but when I ran the Data Connection wizard, only eight of them were visible. After a bit of research, and some trial and error, I discovered that certain web services don't return data that can be used with a DataGrid component. If a web service doesn't have any compatible methods, they won't be displayed in the list. This behavior saves a lot of headache in the long run; however, it can be a bit mind-boggling at first. Of the eight web services I entered that were compatible with the wizard, only about 70 percent of the methods were available.

The wizard has problems with any web services returning ASP.NET DataSets; therefore, services returning a .NET DataSet cannot be used with the wizard. Likewise, a web service won't be able to function with the Data Connection wizard if it only returns a numeric or simple string value.

The Data Connection wizard (and the DataGrid component itself, for that matter) is really only designed to work with a series of similar items, such as an array. Before using the Data Connection wizard, you need to be aware of the following conditions:

  • The wizard doesn't work in every situation. The target XML or web service must return an array for the wizard to work effectively.
  • The wizard only works with Flash MX Professional 2004 because it requires the DataGrid, DataSet, XMLConnector, and WebServiceConnector components.
  • Only arrays with red arrows can be selected in the wizard. Arrays with black arrows indicate that they are nested.
  • If you choose not to create the DataGrid or Button component with the wizard, make sure you add your own Submit button or use ActionScript or a behavior to trigger XMLConnector or WebServiceConnector.
  • If the web service requires parameters passed to it, you need to define the parameters within the wizard or bind values manually after the wizard is closed. A parameter is an item of information that you or the user passes to the operation, which affects how it runs.

Note: Record sets returned by ColdFusion are assigned uppercase column names and each column must be manually added in Step 3 of the wizard.

This example connects you to an ASP.NET web service that grabs current rosters for Major League Baseball teams.

Note: Check out the web service's home page at http://webservices.empowered.com/statsws/stats.asmx.

Before you can use a web service with the wizard, define the web service within the Web Services panel:

  1. Create a new Flash document and open the Web Services panel by selecting Window > Development Panels > Web Services.
  2. Click the Define Web Services button.
  3. In the Define Web Services window click the Add Web Service (+) button.
  4. In the new blank entry, enter the following URL: http://webservices.empowered.com/statsws/stats.asmx?WSDL (see Figure 7).
  5. Click OK to close the Define Web Services window and view the new web service in the Web Services panel.
Defining a new web service in Flash

Figure 7. Defining a new web service in Flash

Now you can expand the web service to view the methods and properties for each defined service. After you are finished, close the Web Services panel and start the Data Connection wizard by choosing Commands > Data Connection Wizard.

Step 1: Choose a Data Source

Here's how you choose the data source:

  1. Select Web Service from the Type of Data Source list box and expand the newly defined web service (called DataService) shown in the list.
  2. Expand the GetPlayersByTeam method and select the Results array. The GetPlayersByTeam method needs to be passed a single parameter, TeamCode, representing the team whose players you want to retrieve. You can add values for the parameters by clicking the Value column for each parameter.
  3. Click the Value column to assign the TeamCode parameter a value of SFO (see Figure 8).
  4. Click Next to proceed.
Choosing a data source and assigning a parameter value for TeamCode

Figure 8. Choosing a data source and assigning a parameter value for TeamCode

Step 2: Choose Data Fields

The rest of the Data Connection wizard is exactly the same as in the previous example using XML, so we'll move a little quicker this time:

  1. Select the data fields PlayerNumber, PlayerName, PlayerPosition, Bats, Throws, and ExperienceYears (see Figure 9). In order to select multiple nonadjacent fields, press the Control (or Command) keys while clicking each field.
  2. Click Next to complete Step 2.
Choosing the data fields you want to display in your movie

Figure 9. Choosing the data fields you want to display in your movie

Step 3: Set Up Data Fields

For the same reasons as in the previous example, skip Step 3 by clicking Next again.

Step 4: Choose Components

  1. Type players into the Label For Generated Components text field.
  2. Uncheck the Create "Get Data" Button option.
  3. Click Finish to close the wizard and return to the Stage.
  4. Just as in the previous example, create a new layer and add the Trigger Data Source behavior to frame 1 to trigger the playersReceive WebServiceConnector component instance.
  5. Test the movie.
  6. After a couple seconds you should see the DataGrid component populated with the San Francisco Giants team roster (see Figure 10).
  7. If you want to adjust headers or column widths, run through the DataGrid Column Editor wizard as you did in the earlier example.
When you test the movie, the data grid populates with the data fields you chose in Step 2 of the wizard

Figure 10. When you test the movie, the data grid populates with the data fields you chose in Step 2 of the wizard

Learning More About the Wizards

Obviously, this article only scratches the surface of what these extensions can do. You can build much more complex applications by writing a bit of code and creating some bindings yourself. Or make simple additions to your existing designs with the extensive range of web services available. If you want to see how these extensions perform with ColdFusion-based web services, read Ben Forta's excellent tutorial, Employee Updater: Building a Client-Server Application with Macromedia Flash Data Connection Wizard and ColdFusion.

With a bit of work, you could extend the examples in this article to allow users to choose which team they want to view rosters for, or view a team's statistics. You could also extend the first example so that clicking on a blog's title views its description. Hopefully this quick overview shows you a little about the newest Macromedia wizards for Flash that allow you to add dynamic content quickly and easily to SWF files.